package com.ruoyi.web.controller.TiebaPosts.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.controller.TiebaForums.domain.TiebaForums;
import com.ruoyi.web.controller.TiebaForums.mapper.TiebaForumsMapper;
import com.ruoyi.web.controller.TiebaPosts.domain.TiebaPosts;
import com.ruoyi.web.controller.TiebaPosts.service.ITiebaPostsService;
import com.ruoyi.web.controller.TiebaPosts.vo.PostDataListVo;
import com.ruoyi.web.controller.TiebaPosts.vo.PostParam;
import com.ruoyi.web.controller.TiebaPosts.vo.TiebaPostsListVo;
import com.ruoyi.web.controller.TiebaPosts.vo.UserTiebaPostsAdd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 贴吧里面的帖子Controller
 *
 * @author yong
 * @date 2025-03-09
 */
@Api(tags = "用户-帖子")
@RestController
@RequestMapping("/TiebaPosts/UserTiebaPosts")
public class UserTiebaPostsController extends BaseController {
    @Autowired
    private ITiebaPostsService tiebaPostsService;

    @Resource
    private TiebaForumsMapper forumsMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @ApiOperation("用户端指定贴吧的所有帖子")
    @GetMapping("/postsList")
    @Anonymous
    public TableDataInfo postsList(TiebaPostsListVo listVo) {
        startPage();
        TiebaPosts tiebaPosts = new TiebaPosts();
        // 获取所有的 帖子数据
        tiebaPosts.setForumId(listVo.getForumId());
        List<TiebaPosts> list = tiebaPostsService.selectTiebaPostsList2(tiebaPosts);
        // 重构数据
        List<PostDataListVo> listVos = new ArrayList<>();
        for (TiebaPosts tiebaPosts1 : list) {
            SysUser sysUser = sysUserMapper.selectUserById(tiebaPosts1.getUserId());
            PostDataListVo postDataListVo = new PostDataListVo();
            postDataListVo.setCreateTime(tiebaPosts1.getCreateTime());
            postDataListVo.setImageUrl(tiebaPosts1.getImageUrl());
            postDataListVo.setContent(tiebaPosts1.getContent());
            postDataListVo.setTitle(tiebaPosts1.getTitle());
            postDataListVo.setCreatBy(sysUser.getUserName());
            postDataListVo.setId(tiebaPosts1.getId());
            postDataListVo.setUserImageUrl(sysUser.getAvatar());
            // 默认值处理
            postDataListVo.setCommentCount(tiebaPosts.getCommentCount() == null ? 0L : tiebaPosts.getCommentCount());
            postDataListVo.setShareCount(tiebaPosts.getShareCount() == null ? 0L : tiebaPosts.getShareCount());
            postDataListVo.setLoveCount(tiebaPosts.getLoveCount() == null ? 0L : tiebaPosts.getLoveCount());
            postDataListVo.setGuanzhuCount(tiebaPosts1.getGuanzhuCount() == null ? 0L : tiebaPosts.getGuanzhuCount());
            postDataListVo.setCollectCount(tiebaPosts1.getCollectCount() == null ? 0L : tiebaPosts.getCollectCount());
            listVos.add(postDataListVo);
        }
        return getDataTable(listVos);
    }


    @ApiOperation("关注/取消关注帖子")
    @PostMapping("/guanzhu")
    public AjaxResult toggleGuanzhu(@RequestBody PostParam param) {
        boolean result = tiebaPostsService.toggleAction(getUserId(), param.getPostId(), 1);
        return AjaxResult.success(result ? "关注成功" : "取消关注成功");
    }

    @ApiOperation("收藏/取消收藏帖子")
    @PostMapping("/collect")
    public AjaxResult toggleCollect(@RequestBody PostParam param) {
        boolean result = tiebaPostsService.toggleAction(getUserId(), param.getPostId(), 2);
        return AjaxResult.success(result ? "收藏成功" : "取消收藏成功");
    }


    /**
     * 新增贴吧里面的帖子
     */
    @ApiOperation("用户端创建帖子")
    @Log(title = "贴吧里面的帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTiebaPostsAdd userTiebaPostsAdd) {
        TiebaPosts tiebaPosts = new TiebaPosts();
        tiebaPosts.setUserId(getUserId());
        tiebaPosts.setDeptId(getDeptId());
        tiebaPosts.setContent(userTiebaPostsAdd.getContent());
        tiebaPosts.setTitle(userTiebaPostsAdd.getTitle());
        tiebaPosts.setForumId(userTiebaPostsAdd.getForumId());
        tiebaPosts.setImageUrl(userTiebaPostsAdd.getImageUrl());

        boolean isAdd = tiebaPostsService.save(tiebaPosts);
        if (isAdd) {
            // 更新贴吧的帖子数量
            TiebaForums forums = forumsMapper.selectById(userTiebaPostsAdd.getForumId());
            if (forums != null) {
                Long currentCount = forums.getForumsCount();
                if (currentCount == null) {
                    currentCount = 0L;
                }
                forums.setForumsCount(currentCount + 1); // 增加帖子数量
                forumsMapper.updateById(forums); // 更新贴吧
            }
            return toAjax(true); // 返回成功
        }
        return toAjax(false); // 返回失败
    }

    /**
     * 修改贴吧里面的帖子
     */
    @ApiOperation("修改帖子")
    @PreAuthorize("@ss.hasPermi('TiebaPosts:TiebaPosts:edit')")
    @Log(title = "贴吧里面的帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTiebaPostsAdd userTiebaPostsAdd) {
        TiebaPosts tiebaPosts = new TiebaPosts();
        tiebaPosts.setUserId(getUserId());
        tiebaPosts.setDeptId(getDeptId());
        tiebaPosts.setContent(userTiebaPostsAdd.getContent());
        tiebaPosts.setTitle(userTiebaPostsAdd.getTitle());
        tiebaPosts.setForumId(userTiebaPostsAdd.getForumId());
        tiebaPosts.setImageUrl(userTiebaPostsAdd.getImageUrl());
        return toAjax(tiebaPostsService.updateById(tiebaPosts));
    }

    /**
     * 删除贴吧里面的帖子
     */
    @ApiOperation("删除帖子")
    @PreAuthorize("@ss.hasPermi('TiebaPosts:TiebaPosts:remove')")
    @Log(title = "贴吧里面的帖子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tiebaPostsService.removeByIds(Arrays.asList(ids)));
    }
}

